package aidturith.files;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet;
import java.util.Set;

public class ProcessFile extends SimpleFileVisitor<Path> {
	
	private String root;
	private Set<File> list = new HashSet<File>();
	
	private Set<String> recognized_ext = new HashSet<String>();
	
	public ProcessFile(String root) throws IOException {
		this.root = root;
		
		build_recognized_extension_list();
		Files.walkFileTree(Paths.get(root), this);
	}
	
	public void build_recognized_extension_list() {
		recognized_ext.add("mp3");
	}
	
	@Override
	public FileVisitResult visitFile(Path aFile, BasicFileAttributes aAttrs) throws IOException {
		for(String ext : recognized_ext) {
			if(aFile.getFileName().toString().contains(ext)) {
				list.add(aFile.toFile());
				return FileVisitResult.CONTINUE;
			}
		}
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult preVisitDirectory(Path aDir, BasicFileAttributes aAttrs) throws IOException {
		return FileVisitResult.CONTINUE;
	}

	public Set<File> getList() { return list; }
	public void setList(Set<File> list) { this.list = list; }
	
	public String getRoot() { return root; }
	public void setRoot(String root) { this.root = root; }

	public static void main(String... aArgs) throws IOException {
		ProcessFile fileProcessor = new ProcessFile("D:\\Musique\\Doujin\\_Comiket 83");
		System.out.println(fileProcessor.getList().size());
		
		for(File f : fileProcessor.getList())
			System.out.println(f.getName());
	}
	
}
	